let App = getApp();
var that;

Page({
	data: {
		canIUse: wx.canIUse('button.open-type.getUserInfo'),
		user: '',
		maskyq: false,
    show:true,
    open_id:''
	},
	onLoad: function() {
		that = this;
    that.userInfo()

		wx.getSetting({
			success: function(res) {
				if (res.authSetting['scope.userInfo']) {
					// 已经授权，可以直接调用 getUserInfo 获取头像昵称
					wx.getUserInfo({
						success: function(res) {
              console.log(res.userInfo)
							that.setData({
								user: res.userInfo,
                show: false

							});
							
						}
					})
				} else {
					
				}
			}
		})

	},
  userInfo() {
    that = this
    wx.request({
      url: App.url.requestUrl + 'user/userInfo',
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded', "token": wx.getStorageSync('access_token') },
      dataType: 'json',
      data: {

      },
      success: function (res) {
        if (res.data.ret == 200) {
          console.log(res.data)
          if (res.data.data) {
            that.setData({
              open_id: res.data.data.open_id,
            })
          }


        }
      }
    })
  },
  isreg() {
    var that = this;
    wx.login({
      success: function (res) {
        console.log(res)
        wx.request({
          url: App.url.requestUrl+'user/checkUserReg',
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          dataType: 'json',
          data: {
            code: res.code
          },
          success: function (lres) {
           
            if (lres.data.ret == 200) {
              wx.setStorageSync('openid', lres.data.data.openid);

            }

          }
        })
      },
    })

  },
  editUser(){
    wx.request({
      url: App.url.requestUrl + 'user/editUser',
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded', "token": wx.getStorageSync('access_token') },
      dataType: 'json',
      data: {
        open_id: wx.getStorageSync("openid"),
        nickname: that.data.user.nickName,
        avatar: that.data.user.avatarUrl,
        gender: that.data.user.gender,
      },
      success: function (res) {


      }
    })
  },
	bindGetUserInfo: function(e) {
    that.setData({
      user: e.detail.userInfo,
      show:false
    });
    that = this
    var open_id = wx.getStorageSync("openid")
    if (open_id){
      that.editUser()
    }else{
      that.isreg() 
      setTimeout(function () {
        that.editUser()
          }, 1500)
     
    }
    



	},
	logout: function() {
		// 确认退出登录
		wx.showModal({
			title: '确定退出登录',
			success: function(res) {
				if (res.confirm) {
					// 退出操作
					wx.clearStorage()
					wx.navigateTo({
            url: '../../login/login/login',

					})


				}
			}
		});
	},
	//弹出邀请码
	yqshow() {
		this.setData({
			maskyq: true
		})
	},
	// 关闭邀请弹窗
	close_yq() {
		this.setData({
			maskyq: false
		})
	},
	// 输入推荐码
	mobile(e) {
		this.setData({
			mobile: e.detail.value
		})
	},
	addInviteCode() {
		that = this;
		if (that.data.mobile == '') {
			wx.showModal({
				title: '请填邀请码',
				showCancel: false
			});
			return;
		}
		wx.request({
			url: App.url.requestUrl + 'user/addInviteCode',
			method: 'POST',
			 header: {'content-type': 'application/x-www-form-urlencoded',"token":wx.getStorageSync('access_token')},
			dataType: 'json',
			data:{mobile:that.data.mobile},
			success: function(res) {
				
				if (res.data.ret == 200) {
					that.setData({
						maskyq: false
					})
					wx.showToast({
						title: '添加成功',
						icon: 'success',
						duration: 2000
					})

				} else {
					that.setData({
						maskyq: false
					})
					wx.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 2000
					})
				}
			}
		})
	},

})
